﻿using System;
using System.Collections.Generic;
using System.Text;
using Mighty.Common;

namespace Mighty.Brain
{
    public class SBrain : BasicBrain
    {
        public override Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            betting.isPass = true;

            // 각 무늬의 개수를 센다
            int heart = 10;
            int clover = 10;
            int spade = 10;
            int diamond = 10;
            int bet = 0;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == Shape.Joker)
                {
                    heart++;
                    clover++;
                    spade++;
                    diamond++;
                }
                if (card.shape == Shape.Clover) 
                {
                    clover++;
                    switch(card.number)
                    {
                        case 10:
                            clover--;
                            break;
                        case 11:
                            clover--;
                            break;
                        case 12:
                            clover--;
                            break;
                        case 13:
                            clover--;
                            break;
                        case 14:
                            clover--;
                            break;
                        default:
                        break;
                   }
                    if (card.number == 14)
                    {
                        clover += 2;
                        if (card.number == 13)
                        {
                            clover += 2;
                            if (card.number == 12)
                            {
                                clover += 2;
                                if (card.number == 11)
                                {
                                    clover += 2;
                                    if (card.number == 10)
                                    {
                                        clover += 2;
                                    }
                                }
                            }
                        }
                    }

                }
                else if (card.shape == Shape.Heart) 
                {
                    heart++;
                    switch(card.number)
                    {
                        case 10:
                            heart--;
                            break;
                        case 11:
                            heart--;
                            break;
                        case 12:
                            heart--;
                            break;
                        case 13:
                            heart--;
                            break;
                        case 14:
                            heart--;
                            break;
                        default:
                        break;
                   }
                    if (card.number == 14)
                    {
                        heart += 2;
                        if (card.number == 13)
                        {
                            heart += 2;
                            if (card.number == 12)
                            {
                                heart += 2;
                                if (card.number == 11)
                                {
                                    heart += 2;
                                    if (card.number == 10)
                                    {
                                        heart += 2;
                                    }
                                }
                            }
                        }
                    }

                }
                else if (card.shape == Shape.Spade)
                {
                    spade++;
                    switch (card.number)
                    {
                        case 10:
                            spade--;
                            break;
                        case 11:
                            spade--;
                            break;
                        case 12:
                            spade--;
                            break;
                        case 13:
                            spade--;
                            break;
                        case 14:
                            spade--;
                            break;
                        default:
                            break;
                    }
                    if (card.number == 14)
                    {
                        spade += 2;
                        diamond += 1;
                        clover += 1;
                        heart += 1;
                        if (card.number == 13)
                        {
                            spade += 2;
                            if (card.number == 12)
                            {
                                spade += 2;
                                if (card.number == 11)
                                {
                                    spade += 2;
                                    if (card.number == 10)
                                    {
                                        spade += 2;
                                    }
                                }
                            }
                        }
                    }

                }
                else if (card.shape == Shape.Diamond)
                {
                    diamond++;
                    switch (card.number)
                    {
                        case 10:
                            diamond--;
                            break;
                        case 11:
                            diamond--;
                            break;
                        case 12:
                            diamond--;
                            break;
                        case 13:
                            diamond--;
                            break;
                        case 14:
                            diamond--;
                            break;
                        default:
                            break;
                    }
                    if (card.number == 14)
                    {
                        diamond += 2;
                        spade += 1;
                        if (card.number == 13)
                        {
                            diamond += 2;
                            if (card.number == 12)
                            {
                                diamond += 2;
                                if (card.number == 11)
                                {
                                    diamond += 2;
                                    if (card.number == 10)
                                    {
                                        diamond += 2;
                                    }
                                }
                            }
                        }
                    }

                }
            }

            // 가장 많은 무늬를 찾아 낸다
            Giruda selectedShape = Giruda.Heart;
            int selectedCount = heart;
            bet = heart;

            if (clover > selectedCount)
            {
                selectedShape = Giruda.Clover;
                selectedCount = clover;
                bet = clover;
            }
            if (spade > selectedCount)
            {
                selectedShape = Giruda.Spade;
                selectedCount = spade;
                bet = spade;
            }
            if (diamond > selectedCount)
            {
                selectedShape = Giruda.Diamond;
                selectedCount = diamond;
                bet = diamond;
            }
            betting.giruda = selectedShape;

            // bet이하로 베팅한다
            if (gameInfo.betting.limit > 0)
            {
                Betting previousBetting = (Betting)gameInfo.bettingHistory[gameInfo.bettingHistory.Count - 1];
                if (previousBetting.limit < bet)
                {
                    betting.isPass = false;
                    betting.limit = bet;
                }
            }
            else
            {
                betting.isPass = false;
                betting.limit = 13;
            }
            return betting;
        }
    }
}
